package com.adlotto2.lcdns.activity.ad.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

import com.adlotto2.lcdns.R;
import com.adlotto2.lcdns.activity.ad.LottoAdJobChoiceActivity;

public class LottoAdJobListAdapter extends BaseAdapter {
	
	private ArrayList<String> mJobListArray, mJobCodeListArray;
	
//	private HashMap<Integer, Boolean> mCheckBoxValueMap;
	
	private SparseBooleanArray mCheckValue;
	
	private ArrayList<Boolean> mCheckedList;
	
	private Context mContext;
	
	private int mCurrentMode;
	
	public LottoAdJobListAdapter(Context con, ArrayList<String> jobListArray) {
		mJobListArray = jobListArray;
		mContext = con;
	}
	
	public LottoAdJobListAdapter(Context con, int mode) {
		mContext = con;
		mCurrentMode = mode;
		mJobListArray = new ArrayList<String>();
		mJobCodeListArray = new ArrayList<String>();
//		mCheckBoxValueMap = new HashMap<Integer, Boolean>();
		mCheckValue = new SparseBooleanArray();
	}
	
	public void setJobNameListArray(ArrayList<String> jobList) {
		mJobListArray = jobList;
		for(int i = 0; i < mJobListArray.size(); i++) {
//			mCheckBoxValueMap.put(i, false);
			mCheckValue.put(i, false);
		}
	}
	
	public void setJobCodeListArray(ArrayList<String> jobCode) {
		mJobCodeListArray = jobCode;
	}

	@Override
	public int getCount() {
		return mJobListArray.size();
	}

	@Override
	public Object getItem(int position) {
		return mJobListArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public int getCheckedItemCount() {
		int result = -1;
		if(mCurrentMode == LottoAdJobChoiceActivity.MODE_REQUEST) {
			result = 0;
			for(int i = 0; i < getCount(); i++) {
				if(mCheckValue.get(i)) {
					result++;
				}
			}
		}
		return result;
	}
	
	public String getCheckedItemsCode() {
		String result = null;
		boolean flag = false;
		for(int i = 0; i < getCount(); i++) {
			if(mCheckValue.get(i) && !flag) {
				result = mJobCodeListArray.get(i);
				flag = true;
			} else if(mCheckValue.get(i) && flag) {
				result = result + "," + mJobCodeListArray.get(i);
			}
		}
		return result;
	}
	
	public String getCheckedItemsName() {
		String result = null;
		boolean flag = false;
		for(int i = 0; i < getCount(); i++) {
			if(mCheckValue.get(i) && !flag) {
				result = mJobListArray.get(i);
				flag = true;
			} else if(mCheckValue.get(i) && flag) {
				result = result + "," + mJobListArray.get(i);
			}
		}
		return result;
	}
	
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		if(view == null) {
			if(mCurrentMode == LottoAdJobChoiceActivity.MODE_REQUEST) {
				view = inflater.inflate(R.layout.lottoad_job_list_column_check, null);
			} else {
				view = inflater.inflate(R.layout.lottoad_job_list_column, null);
			}
		}
		final int posi = position;
		final TextView jobName = (TextView) view.findViewById(R.id.tv_job_list_content);
		final CheckBox chkBox;
		
		if(mCurrentMode == LottoAdJobChoiceActivity.MODE_REQUEST) {
			/*chkBox = (CheckBox) view.findViewById(R.id.cb_job_list);
			if(mCheckBoxValueMap.get(posi) == true) {
				chkBox.setChecked(true);
				Log.e("BenTag", posi + " isChecked true");
			}
			
			chkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					// TODO Auto-generated method stub
					mCheckBoxValueMap.put(posi, isChecked);
					if(!CNLottoAdApiSet.isLive()) {
						if(isChecked) {
							Toast.makeText(mContext, jobName.getText().toString() + "선택됨", Toast.LENGTH_SHORT).show();
						} else {
							Toast.makeText(mContext, jobName.getText().toString() + "해제됨", Toast.LENGTH_SHORT).show();
						}
					}
				}
			});*/
			chkBox = (CheckBox) view.findViewById(R.id.cb_job_list);
			if(mCheckValue.get(posi) == true) {
				chkBox.setChecked(true);
			} else {
				chkBox.setChecked(false);
			}
			
			chkBox.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(mCheckValue.get(posi)) {
						mCheckValue.put(posi, false);
						chkBox.setChecked(false);
					} else {
						mCheckValue.put(posi, true);
						chkBox.setChecked(true);
					}
					
				}
			});
		}
		
		jobName.setText(mJobListArray.get(position));
		
		return view;
	}

}
